<template>
  <node
    :title="config.name"
    :show-error="showError"
    :content="content"
    :error-info="errorInfo"
    @selected="$emit('selected')"
    @delNode="$emit('delNode')"
    @insertNode2="(type) => $emit('insertNode', type)"
    placeholder="请设置审批人"
    header-bgc="#ff943e"
    header-icon="el-icon-s-check"
  />
</template>

<script setup name="ApprovalNode" lang="ts">
import Node from "./Node";
import { NODE_MODEL } from "@/components/Process/DefaultNodeProps";
import { any } from "vue-types";

const props = defineProps({
  config: {
    type: Object,
    default: () => {
      return {};
    }
  }
})



const config = ref(props.config)

const showError = ref(false)
const errorInfo= ref("")

const content= computed(():string => {
  const config = props.config.props;
  switch (config.assignedType) {
    case "ASSIGN_USER":
      if (config.assignedUser.length > 0) {
        let texts: any[] = [];
        config.assignedUser.forEach((org: { name: any; }) => texts.push(org.name));
        return String(texts).replaceAll(",", "、");
      } else {
        return "请指定审批人";
      }
    case "SELF":
      return "发起人自己";
    case "SELF_SELECT":
      return config.selfSelect.multiple ? "发起人自选多人"
        : "发起人自选一人";
    case "LEADER_TOP":
      return "多级主管依次审批";
    case "LEADER":
      return config.leader.level > 1 ? "发起人的第 " + config.leader.level + " 级主管"
        : "发起人的直接主管";
    case "FORM_USER":
      if (!config.formUser || config.formUser === "") {
        return "表单内联系人（未选择）";
      } else {
        let text = getFormItemById(config.formUser);
        if (text && text.title) {
          return `表单（${text.title}）内的人员`;
        } else {
          return "该表单已被移除😥";
        }
      }
    case "ROLE":
      if (config.role.length > 0) {
        return String(config.role).replaceAll(",", "、");
      } else {
        return "指定角色（未设置）";
      }
    default:
      return "未知设置项😥";
  }
})

const getFormItemById = (id:any)=>{
  return NODE_MODEL.design.formItems.find((item: { id: any; }) => item.id === id);
}

// const validate_ASSIGN_USER = ()=>{
//   return true;
// }
// const validate_SELF_SELECT = ()=>{
//   return true;
// }
// const validate_LEADER_TOP = ()=>{
//   return true;
// }
// const validate_LEADER = ()=>{
//   return true;
// }
// const validate_ROLE = ()=>{
//   return true;
// }
// const validate_SELF = ()=>{
//   return true;
// }
// const validate_REFUSE = ()=>{
//   return true;
// }

const methodMap = computed(()=> {
  var map = new Map<string,any>;
  map.set("validate_ASSIGN_USER", () => {
    return true;
  });
  map.set("validate_SELF_SELECT", () => {
    return true;
  });
  map.set("validate_LEADER_TOP", () => {
    return true;
  });
  map.set("validate_LEADER", () => {
    return true;
  });
  map.set("validate_ROLE", () => {
    return true;
  });
  map.set("validate_SELF", () => {
    return true;
  });
  map.set("validate_REFUSE", () => {
    return true;
  });
  return map;
})

const validate = () => {
  try {
    let funcName = `validate_${config.value.props.assignedType}`;
    const action = methodMap.value.get(funcName);
    if (action) {
      var actionResult = action();
      return (showError.value = !actionResult);
    } else {
      return true;
    }
  } catch (e) {
    return true;
  }
};
// 导出方法
defineExpose({validate})
</script>

<style scoped></style>
